package com.yanqu.road.server.manager.activity.xingyou.inter.grid;

import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamActionLogData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.activity.youxing.ShanShuiGridParam;
import com.yanqu.road.entity.activity.youxing.config.XingYouShanShuiGridInfo;
import com.yanqu.road.entity.log.cross.xingyou.LogCrossXingYouBaoHua;
import com.yanqu.road.entity.log.cross.xingyou.LogCrossXingYouExplore;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.manager.activity.xingyou.ConfirmHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.DiceHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.enums.eXingYouGridTypes;
import com.yanqu.road.server.manager.activity.xingyou.inter.GridHandleResult;
import com.yanqu.road.server.manager.activity.xingyou.inter.IXingYouGridHandler;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Arrays;

/**
 * 探索/高僧礼格
 */
public class ExplortGridHandler implements IXingYouGridHandler {

    @Override
    public void handle(XingYouShanShuiGridInfo gridInfo, int pos, int gridId, int endPos, boolean fly, DiceHandleResult result) {
        //是否停留
        boolean stop = pos == endPos;
        if(stop){
            ShanShuiGridParam gridParam = new ShanShuiGridParam(gridId, pos, false, result.getUserData().isDrift());
            GridHandleResult gridHandleResult = new GridHandleResult();
            gridHandleResult.setGridParam(gridParam);

            Property reward = new Property();
            Property fixedReward = result.getActivity().getGridInfoFixedReward(result.getUserData(), gridInfo, true);

            XingYouShanShuiProto.ShanShuiEventInfo.Builder eventBuilder = result.getActivity().getEventInfoBuilder(pos, gridId, reward, false);
            reward.addProperty(fixedReward);
            eventBuilder.setBaseReward(PropertyHelper.parsePropertyToString(reward));
            result.getActivity().calRewardBuff(result.getUserData(), reward, false, 0);


            eventBuilder.setFixedReward(PropertyHelper.parsePropertyToString(fixedReward));
            eventBuilder.setAward(PropertyHelper.parsePropertyToString(reward));

            gridHandleResult.setBuilder(eventBuilder);
            result.getGridHandleResultList().add(gridHandleResult);

            CrossXingYouShanShuiUserData userData = result.getUserData();
            //是否有捕快
            result.getActivity().tryCatch(pos, userData, result, gridHandleResult);
        }
    }

    @Override
    public void confirm(XingYouShanShuiGridInfo gridInfo, CrossXingYouShanShuiUserData userData, ConfirmHandleResult handleResult, long select, boolean isDrift) {
        ShanShuiGridParam gridParam = userData.getGridParam();
        if(userData.isDrift()){
            gridParam = userData.getDriftGridParam();
        }
        if(gridParam == null || gridParam.isHandled()){
            //参数问题
            handleResult.setRet(GameErrorCode.E_XING_YOU_SHAN_SHUI_GRID_HANDLED);
            return;
        }
        if(gridInfo.getId() == eXingYouGridTypes.Five.getId()){
            //高僧赠礼
            if(select != 1 && select != 2){
                handleResult.setRet(GameErrorCode.E_REQUEST_ARGUMENT_ERROR);
                return;
            }
            Property property;
            if(select == 1){
                property = RandomWeightConfigHelper.getProperty(gridInfo.getReward1());
                handleResult.getBuilder().setBaseReward(PropertyHelper.parsePropertyToString(property));
                handleResult.getActivity().calRewardBuff(userData, property, false, 0);

                handleResult.getReward().addProperty(property);
            }else {
                property = RandomWeightConfigHelper.getProperty(gridInfo.getReward2());
                handleResult.getBuilder().setBaseReward(PropertyHelper.parsePropertyToString(property));
                handleResult.getActivity().calRewardBuff(userData, property, false, 0);

                handleResult.getReward().addProperty(property);
            }
            gridParam.setHandled(true);
            userData.setUpdateOption();

            //队伍日志：经过高僧赠礼
            CrossXingYouShanShuiTeamActionLogData actionLogData = handleResult.getActivity().createActionLogData(userData, XingYouShanShuiProto.ShanShuiLogTypeEnum.SHAN_SHUI_LOG_TREASURE,
                    PropertyHelper.parsePropertyToString(property), Arrays.asList(String.valueOf(userData.getRoleId()), String.valueOf(gridInfo.getId())));

            handleResult.getActionLogDataList().add(actionLogData);

            LogCrossXingYouExplore logData = new LogCrossXingYouExplore(userData.getActivityId(), userData.getTeamId(), userData.getUserId(), (int)select,
                    property.getCountByGoodsId(handleResult.getActivity().getConfig().getSHANSHUI_SILVER_COIN_ID()).intValue(),
                    property.getCountByGoodsId(handleResult.getActivity().getConfig().getSHANSHUI_GOLD_COIN_ID()).intValue(),
                    PropertyHelper.parsePropertyToString(property));
            logData.setGroupId(userData.getGroupId());
            handleResult.getAutoLogList().add(logData);

        }else if(gridInfo.getId() == eXingYouGridTypes.Four.getId() || gridInfo.getId() == eXingYouGridTypes.EXPLORE.getId()){
            //探索 九宫格
            Property property = RandomWeightConfigHelper.getProperty(gridInfo.getReward1());
            handleResult.getBuilder().setBaseReward(PropertyHelper.parsePropertyToString(property));
            handleResult.getActivity().calRewardBuff(userData, property, false, 0);

            handleResult.getReward().addProperty(property);
            gridParam.setHandled(true);
            userData.setUpdateOption();

            //队伍日志：发现格
            CrossXingYouShanShuiTeamActionLogData actionLogData = handleResult.getActivity().createActionLogData(userData, XingYouShanShuiProto.ShanShuiLogTypeEnum.SHAN_SHUI_LOG_DISCOVER,
                    PropertyHelper.parsePropertyToString(property), Arrays.asList(String.valueOf(userData.getRoleId()), String.valueOf(gridInfo.getId())));

            handleResult.getActionLogDataList().add(actionLogData);

            LogCrossXingYouExplore logData = new LogCrossXingYouExplore(userData.getActivityId(), userData.getTeamId(), userData.getUserId(), (int)select,
                    property.getCountByGoodsId(handleResult.getActivity().getConfig().getSHANSHUI_SILVER_COIN_ID()).intValue(),
                    property.getCountByGoodsId(handleResult.getActivity().getConfig().getSHANSHUI_GOLD_COIN_ID()).intValue(),
                    PropertyHelper.parsePropertyToString(property));
            logData.setGroupId(userData.getGroupId());
            handleResult.getAutoLogList().add(logData);
        }
    }
}
